import unittest
from time import sleep

from Finance_fun.page.page01_register import Page
from Finance_fun.base.get_driver import GetDriver
from Finance_fun.tool.read_json import read_json
from parameterized import parameterized
from Finance_fun.tool import assert_rl


def get_register_data():
    case = read_json("data01_register_case.json")
    list_null = []
    for data in case.values():
        list_null.append(
            (
                data["phone"],
                data["password"],
                data["verify_code"],
                data["phone_code"],
                data["expect"]
             )
        )
    return list_null


class RegiterTest(unittest.TestCase):
    @classmethod
    def setUpClass(cls):
        cls.driver = GetDriver.get_driver()
        cls.register = Page(cls.driver)
        cls.register.page_click_register_link()

    @classmethod
    def tearDownClass(cls):
        GetDriver.quit_driver()

    @parameterized.expand(get_register_data())
    def test_register(self, phone, password, verify_code, phone_code, expect):
        self.driver.refresh()
        try:
            if expect in ["验证码过期或无效，请重新获取"]:
                self.register.page_register_no_click(phone, password, verify_code, phone_code)
                assert_rl.assert_register_phone_code_err(self, expect)
            elif expect in "'你好，' + phone":
                self.register.page_register(phone, password, verify_code, phone_code)
                sleep(5)
                assert_rl.assert_register_success(self, expect)
            elif expect not in ["验证码过期或无效，请重新获取"]:
                self.register.page_register(phone, password, verify_code, phone_code)
                if expect in ["手机号码不能为空", "手机号码格式错误", "手机已存在!"]:
                    assert_rl.assert_register_phone_err(self, expect)
                elif expect in ["密码不能为空", "不能输入纯数字", "不能输入纯字母", "密码长度6~16之间且密码为英文、数字的组合"]:
                    assert_rl.assert_register_password_err(self, expect)
                elif expect in ["验证码不能为空", "请输入4位验证码", "验证码错误"]:
                    assert_rl.assert_register_verify_code_err(self, expect)
                elif expect in ["短信验证码不能为空"]:
                    assert_rl.assert_register_phone_code_err(self, expect)
        except Exception:
            self.register.page_get_screenshot()
            raise
